﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MossmanMill.DataModel;
using System.Windows.Forms;
using MossmanMill.Properties;
using System.Drawing;
using MossmanMill.View;
using System.Diagnostics;

namespace MossmanMill
{
    partial class MainForm
    {
        public static MainForm Instance;

        private Level level;
        private DatabaseAccess db;
        private List<Room> rooms;
        private Sensor[] sensors;
        private Graphics g;
        private Bitmap buffer;

        enum Level
        {
            ROOM,
            RACK,
            CARD,
            SENSOR
        }

        public void Init()
        {
            db = DatabaseAccess.Instance;
            Instance = this;
            buffer = new Bitmap(viewPanel.Width, viewPanel.Height);
            g = Graphics.FromImage(buffer);
            InitRoom();
            Invalidate();
        }

        private void OnPaint(object sender, PaintEventArgs e)
        {
            try
            {
                g.Clear(Color.AliceBlue);
                switch (level)
                {
                    case Level.ROOM:
                        DrawRoom(g);
                        break;
                    case Level.RACK:
                        DrawRack(g);
                        break;
                    case Level.CARD:
                        DrawCard(g);
                        break;
                }
                e.Graphics.DrawImage(buffer, 0, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void MouseDownEvent(object sender, MouseEventArgs e)
        {
            OnClick(e.Location, 0);
        }

        private void MouseUpEvent(object sender, MouseEventArgs e)
        {
            OnClick(e.Location, 2);
        }

        private void MouseMoveEvent(object sender, MouseEventArgs e)
        {
            OnClick(e.Location, 1);
        }

        private void OnClick(Point point, int type)
        {
            switch (level)
            {
                case Level.ROOM:
                    ClickRoom(point, type);
                    break;
                case Level.RACK:
                    ClickRack(point, type);
                    break;
                case Level.CARD:
                    ClickCard(point, type);
                    break;
            }
        }

        private void DrawBackButton(Graphics g)
        {
            g.DrawImage(Resources.button, backRect);
            g.DrawString("Back", Fonts.mediumArial, Brushes.Black, backRect.Left + 20, backRect.Top + 5);
        }

    }
}
